using System.Drawing;
using System.Drawing.Drawing2D;

namespace AsposeTools
{
	/// <summary>
	/// Demansions for ruler bar.
	/// </summary>
	public enum RulerBarUnits
	{
		/// <summary>
		/// Specifies 1/75 inch as the unit of measure.
		/// </summary>
		Display,
		/// <summary>
		/// Specifies the document unit (1/300 inch) as the unit of measure.
		/// </summary>
		Document,
		/// <summary>
		/// Specifies the inch as the unit of measure.
		/// </summary>
		Inch,
		/// <summary>
		/// Specifies the millimeter as the unit of measure.
		/// </summary>
		Millimeter,
		/// <summary>
		/// Specifies the centimeter as the unit of measure.
		/// </summary>
		Centimeter,
		/// <summary>
		/// Specifies a device pixel as the unit of measure.
		/// </summary>
		Pixel,
		/// <summary>
		/// Specifies a printer's point (1/72 inch) as the unit of measure.
		/// </summary>
		Point,
	}
	
	/// <summary>
	/// Metrics Converter
	/// </summary>
	public class MetricsConverter
	{
		private static readonly float[] m_Proportions;
    
		static MetricsConverter()
		{
			Bitmap   _bmp = new Bitmap( 1, 1 );
			Graphics _graph = Graphics.FromImage( _bmp );
      
			PointF[] points = new []{ new PointF( 1, 1 ) };
      
			GraphicsContainer cont = _graph.BeginContainer(
				new Rectangle( 0, 0, 1, 1 ), new Rectangle( 0, 0, 1, 1 ),
				GraphicsUnit.Pixel );
      
			_graph.PageUnit = GraphicsUnit.Inch;
			_graph.TransformPoints( CoordinateSpace.Device, CoordinateSpace.Page, points );
			_graph.EndContainer( cont );
      
			float DEF_INCH_PIXEL = points[ 0 ].X;
      
			m_Proportions = new [] {
					DEF_INCH_PIXEL / 75,     // Display
					DEF_INCH_PIXEL / 300,    // Document
					DEF_INCH_PIXEL,          // Inch
					DEF_INCH_PIXEL / 25.4f,  // Millimeter
					DEF_INCH_PIXEL / 2.54f,  // Centimeter
					1,                       // Pixel
					DEF_INCH_PIXEL / 72,     // Point
			};
		}
		/// <summary>
		/// Converts value, stored in "from" untis, to pixels
		/// </summary>
		/// <param name="value">Value to convert</param>
		/// <param name="from">Indicates units to convert from</param>
		/// <returns>Value stored in pixels</returns>
		public static float ConvertToPixels( float value, RulerBarUnits from )
		{
			return value * m_Proportions[ ( int )from ];
		}

		/// <summary>
		/// Converts value, stored in pixels, to value in "to" units
		/// </summary>
		/// <param name="value">Value to convert</param>
		/// <param name="to">Indicates units to convert to</param>
		/// <returns>Value stored in "to" units</returns>
		public static float ConvertFromPixel( float value, RulerBarUnits to )
		{
			return value / m_Proportions[ ( int )to ];
		}

		/// <summary>
		/// Converts value, stored in "from" units, to value in "to" units
		/// </summary>
		/// <param name="value">Value to convert</param>
		/// <param name="from">Indicates units to convert from</param>
		/// <param name="to">Indicates units to convert to</param>
		/// <returns>Value stored in "to" units</returns>
		public static float ConvertUnits( float value, RulerBarUnits from, RulerBarUnits to )
		{
			return ConvertFromPixel( ConvertToPixels( value, from ), to );
		}
	}
}
